package com.hxzy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.anno.OperateLog;
import com.hxzy.exception.EmpAccountLockedException;
import com.hxzy.mapper.EmpMapper;
import com.hxzy.pojo.Emp;
import com.hxzy.pojo.PageBean;
import com.hxzy.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wzm
 * @create 2023-10-07 16:44
 * project_name:stu_sys
 */
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    /*
    原始分页
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1.获取总记录数total
        Long total = empMapper.count();

        //2.获取当前页的查询数据列表rows
        Integer begin = (page-1)*pageSize;//计算起始索引：(页码-1)*pageSize
       List<Emp> rows = empMapper.listByPage(begin,pageSize);

        //3.返回查询后的分页对象
        return new PageBean(total,rows);
    }
    */

    //pagehelper
    @Override
    public PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        //1.设置分页的参数
        PageHelper.startPage(page, pageSize);
        //2.执行分页操作（不需要自己指定limit）
        List<Emp> list = empMapper.list(name,gender,begin,end);

        //3.获取分页后相关信息
        //获取Page对象(现在比较推荐)
        Page<Emp> p = (Page<Emp>) list;
        log.info("page对象：{}",p);


        //3.返回查询后的分页对象
       return new PageBean(p.getTotal(),p.getResult());

    }

    @Override
    @OperateLog
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    @OperateLog
    public void save(Emp emp) {
        //补全时间信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //调用mapper完成功能
        empMapper.insert(emp);
    }

    @Override
    public Emp findById(Integer id) {
        return empMapper.selectById(id);
    }

    @Override
    @OperateLog
    public void update(Emp emp) {
        //补全修改时间
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);

    }

    @Override
    public Emp login(Emp emp) {
        Emp loginEmp = empMapper.selectByUserNameAndPassWord(emp);
//        if(true){
//            throw new EmpAccountLockedException("账户被锁定了");
//        }
        return loginEmp;
    }

}
